<?php
class Dmdform extends AppModel {
	var $name = 'Dmdform';
	var $displayField = 'code';
	//The Associations below have been created with all possible keys, those that are not needed can be removed
var $virtualFields=array(
            'display'=>'CONCAT(Dmdform.code)'
        );

 

	var $belongsTo = array(
		'Dmdformtype' => array(
			'className' => 'Dmdformtype',
			'foreignKey' => 'dmdformtype_id',
		),
		'Demander' => array(
			'className' => 'Demander',
			'foreignKey' => 'demander_id',
		),
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
		),
                'UserHrreferent' => array(
			'className' => 'User',
			'foreignKey' => 'user_hrreferent_id',
		),
		'Backup' => array(
			'className' => 'Backup',
			'foreignKey' => 'backup_id',
		),
		'Manager' => array(
			'className' => 'Manager',
			'foreignKey' => 'manager_id',
		),
		'ProfileReference' => array(
			'className' => 'User',
			'foreignKey' => 'profile_reference_id',
		),
		'UserStatus' => array(
			'className' => 'Status',
			'foreignKey' => 'user_status_id',
		),
                'Jobprofile1' => array(
			'className' => 'Jobprofile',
			'foreignKey' => 'user_jobprofile1_id',
		),
                'Jobprofile2' => array(
			'className' => 'Jobprofile',
			'foreignKey' => 'user_jobprofile2_id',
		),
                'Jobprofile3' => array(
			'className' => 'Jobprofile',
			'foreignKey' => 'user_jobprofile3_id',
		),
                'Jobfunction' => array(
			'className' => 'Jobfunction',
			'foreignKey' => 'user_jobfunction_id',
		),
                'Company' => array(
			'className' => 'Company',
			'foreignKey' => 'company_id',
		),

	);

	var $hasMany = array(
		'Action' => array(
			'className' => 'Action',
			'foreignKey' => 'dmdform_id',
			'dependent' => false,

		),
		'Formstatus' => array(
			'className' => 'Formstatus',
			'foreignKey' => 'dmdform_id',
			'dependent' => false,

		),
                'Baali' => array(
			'className' => 'Baali',
			'foreignKey' => 'tool_id',
			'dependent' => false
		)
                
	);

        function getJobListes(){
            $jobprofiles=$this->Jobprofile1->find('list');
            $jobfunctions=$this->Jobfunction->find('list');
            $branches=$this->User->Powerdelegation->Workflow->find('list',array('fields'=>array('branch')));
            $costcenters=$this->User->Powerdelegation->Workflow->find('list',array('fields'=>array('costcenter')));
            $joblists=array(
              'jobprofiles'=>$jobprofiles,
              'jobfunctions'=>$jobfunctions,
              'branches'=>$branches,
              'costcenters'=>$costcenters,
            );
            return $joblists;
        }
        function getWkListe(){
            $workflows=$this->User->Powerdelegation->Workflow->find('list');
            return $workflows;
        }
        function getUsers(){
            $users=$this->User->find('list',array('fields'=>array('UsName')));
            return $users;
        }
        function getCompanies(){
            $Companies=$this->Company->find('list',array('fields'=>array('fullRow')));
            return $Companies;
        }

        function getModules($idType=null){
            if($idType){
                return $this->Dmdformtype->getModulesForDmdformType($idType);
            }else{
                return false;
            }
        }
        function getModulesGetActions($idType=null){
            if($idType){
                return $this->Dmdformtype->getModulesGetActionsForDmdformType($idType);
            }else{
                return false;
            }
        }
        function getModulesValidation($idType=null){
            if($idType){
                return $this->Dmdformtype->getModulesValidationForDmdformType($idType);
            }else{
                return false;
            }
        }
        function getModuleMenuNames($idType=null){
            if($idType){
                return $this->Dmdformtype->getModuleTitleNames($idType);
            }else{
                return false;
            }
        }
        function getType($idType=null){
            return $this->Dmdformtype->getTitle($idType);
        }
        function getTypeId($dmdid){
            $d= $this->find('list',array('conditions'=>array('id'=>$dmdid),'fields'=>array('dmdformtype_id')));
            return $d[$dmdid];
        }
        function getUser($userId=null){
            return $this->User->getUser($userId);
        }
        function getTools(){
            return $this->User->Powerdelegation->Workflow->getTools();
        }
        function getToolsTables(){
            return $this->User->Powerdelegation->Workflow->getToolsTables();
        }
        function getDmdform($DmdId){
            return $this->find('first',array('conditions'=>array('id'=>$DmdId)));
        }
        function getDmdforms(){
            return $this->find('all');
        }
        function getLastId(){
            return $this->find('count');
        }
        function saveData($id,$data){
        
            $this->id=$id;

            $this->save($data);
        }
        function getUserMail($userId){
            return $this->User->getUserMail($userId);
        }
        function getCode($dmdid){
            $d= $this->find('list',array('conditions'=>array('id'=>$dmdid),'fields'=>array('code')));
            return $d[$dmdid];
        }
        
        function getFormData($dmdid){
            $d= $this->find('list',array('conditions'=>array('id'=>$dmdid),'fields'=>array('formdata')));
            return $d[$dmdid];
        }
        function getItMailsForTools($dmdId){
            $mails=array();
            $toolsIdList=$this->find('list',array(
                'conditions'=>array(
                    'id'=>$dmdId
                ),
                'fields'=>array('tool_ids')
            ));
            $toolsIdList=$toolsIdList[$dmdId];
            $toolsIdList=split(";",$toolsIdList);
            foreach ($toolsIdList as $toolId){
                $mail=$this->Jobprofile1->Tool->find('list',array(
                    'conditions'=>array(
                        'Tool.id'=>$toolId
                    ),
                    'fields'=>array('admin_mail')
                ));
                $mails=array_merge($mails,$mail);
            }
            return $mails;
        }
        function getToolAdminArray($dmdId){
            
            $toolsIdList=$this->find('list',array(
                'conditions'=>array(
                    'id'=>$dmdId
                ),
                'fields'=>array('tool_admin')
            ));
            return $toolsIdList[$dmdId];
            
        }
        function createAction($type,$dmdId,$by,$status){

        ($dmdId>0)?$dmdCode=  $this->getCode($dmdId):$dmdCode=0;
        $this->Action->createAction($type,$dmdId,$by,$dmdCode,$status);
    }
        function getManagerId($id){
            $mid=  $this->find('list',array(
            'conditions'=>array('id'=>  $id),
            'fields'=>array('manager_id')
            ));
           return $mid[$id];
            
        }
        function getHrId($id){
            $mid=  $this->find('list',array(
            'conditions'=>array('id'=>  $id),
            'fields'=>array('user_hrreferent_id')
            ));
           return $mid[$id];
            
        }
        function getManagerVisa($id){
             $val=$this->find('list',array(
                'conditions'=>array('id'=>  $id),
                'fields'=>array('manager_visa')
            ));
            return $val[$id];
        }
        
        function getCheckMail($id){
            $c=array();
            $c['mid']=$this->getManagerId($id);
            $c['hrid']=$this->getHrId($id);
            $c['val']=$this->getManagerVisa($id);
            return $c;
        }
}
?>